<template>
  <div class="login-container">
    <div class="elform">
      <el-form
        ref="loginForm"
        :rules="rules"
        :model="form"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3>
            <img class="title" src="@/assets/logo.595745bd.png" alt="">
          </h3>
        </div>
        <!-- 号码 -->
        <el-form-item prop="loginName">
          <span class="svg-container el-icon-user" />
          <el-input v-model="form.loginName" placeholder="请输入手机号码" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            ref="pwdinp"
            v-model="form.password"
            :type="passwordType"
            placeholder="请输入密码"
          />
          <span class="svg-container" @click="change">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <span class="svg-container el-icon-star-off" />
          <el-input ref="code" v-model="form.code" placeholder="请输入验证码" maxlength="4" />

          <img
            class="clientToken_img"
            :src="codeUrl"
            alt=""
            @click="getCode"
          >
        </el-form-item>
        <el-button
          :loading="loading"
          class="loginBtn"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click="submit"
        >登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      form: {
        loginName: 'admin',
        password: 'admin',
        clientToken: '',
        loginType: 0,
        code: ''
      },
      passwordType: 'password',
      rules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
          // {pattern:/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/, message: '手机号格式错误', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 16, message: '请输入正确的密码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码必填', trigger: 'blur' }
        ]
      },
      codeUrl: ''
    }
  },

  mounted() {
    this.getCode()
  },
  methods: {

    change() {
      this.passwordType === 'password' ? this.passwordType = 'text' : this.passwordType = 'password'
      this.$nextTick(() => {
        this.$refs.inputFocus.focus()
      })
    },
    async submit() {
      try {
        this.loading = true
        await this.$refs.loginForm.validate()
        console.log('拿到token')
        await this.$store.dispatch('user/loginAction', this.form)
        this.loading = false
        this.$router.push('/')
      } catch (e) {
        console.log(e)
        this.loading = false
      }
    },
    async getCode() {
      this.form.code = ''
      const random = Math.random() * 100
      const res = await axios({
        url: `https://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${random}`,
        responseType: 'blob'
      })
      console.log(res, 'res到底是什么')
      this.codeUrl = res.config.url
      this.form.clientToken = random
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #83878c; // 将输入框颜色改成蓝色;
$cursor: deeppink;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance:none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(25, 25, 25, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }
  // 设置错误信息的颜色和表单背景色文字颜色放置一起
  .el-form-item__error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #68b0fe; // 将输入框颜色改成蓝色;

.login-container {
  background-image: url("~@/assets/background.be4fae7d.png");
  background-position: center;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  position: relative;
  .elform{
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
    .login-form {
      width: 518px;
      height: 388px;
      top: -100px;
      left: 5px;
      position: absolute;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
    }
    .loginBtn {
      background: linear-gradient(262deg, #2e50e1, #6878f0);
      height: 52px;
      line-height: 32px;
      font-size: 14px;
      border-radius: 8px;
      color: #fff;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: absolute;
      margin-left: 165px;
      margin-top: -125px;
    h3{
      img{
    width: 96px;
    height: 96px;
    top: 46px;
    left: 50%;
      }
    }
      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

  }
  .el-form-item__content{
    position: relative;
    overflow: hidden;
    .clientToken_img{
      position: absolute;
      width: 130px;
      height: 50px;
      // background-color: #2e50e1;
      left: 320px;
      border: 1px solid #08b908;
    }
  }

}
</style>
